﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Net;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Collections.Specialized;
using System.Web;
using System.Text;
using System.Linq;
using Senparc.Weixin.MP.Entities;

namespace WeiXinPost
{
    static class Program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [STAThread]
        static void Main()
        {
            //Youdao();

            //Chongdong();

            baidutianqi();

            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new frmMain());
        }

        private static void baidutianqi()
        {
            string url = "http://api.map.baidu.com/telematics/v3/weather?location=%E6%9D%AD%E5%B7%9E&ak=g86PLcv0YlSUp70IKYDlDNrG&output=json";
            WebRequest request = WebRequest.Create(url);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            string responseFromServer = string.Empty;
            using (Stream reponseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(reponseStream);
                responseFromServer = reader.ReadToEnd();
            }

            ResponseMessageNews a = new ResponseMessageNews();
            a.Articles.Add(new Article { Title = "ddd" });

            JObject j = JObject.Parse(responseFromServer);
           
            JToken jt = j["results"][0];
            JArray jr = jt["weather_data"] as JArray;
            a.Articles.AddRange(jr.Select(x =>
            {
                return new Article
                {
                    PicUrl = x.Value<string>("dayPictureUrl"),
                    Title = x.Value<string>("date") + System.Environment.NewLine
                        + x.Value<string>("weather") + "  "
                        + x.Value<string>("wind") + "  "
                        + x.Value<string>("temperature"),
                };
            }));
        }

        private static void Chongdong()
        {
            string url = "http://wap.unidust.cn/api/searchout.do";

            //WebClient client = new WebClient();
            //client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
            //NameValueCollection postVals = new NameValueCollection();
            //postVals.Add("info", "笑话");
            //postVals.Add("type", "client");
            //postVals.Add("ch", "1001");
            //byte[] byteData = client.UploadValues(url, "POST", postVals);
            //string response = System.Text.Encoding.UTF8.GetString(byteData);

            //WebRequest request = WebRequest.Create(url);
            //request.Method = "POST";
            //request.ContentType = "application/x-www-form-urlencoded";
            //string postData = "info=" + HttpUtility.UrlEncode("笑话")
            //    + "&type=wap"
            //    + "&ch=1001";
            //byte[] byteArray = Encoding.UTF8.GetBytes(postData);
            //request.ContentLength = byteArray.Length;
            //using (Stream requestStream = request.GetRequestStream())
            //{
            //    requestStream.Write(byteArray, 0, byteArray.Length);
            //}
            //WebResponse response = request.GetResponse();
            //using (Stream reponseStream = response.GetResponseStream())
            //{
            //    StreamReader reader = new StreamReader(reponseStream);
            //    string responseFromServer = reader.ReadToEnd();
            //}

            //url = "http://wap.unidust.cn/api/searchout.do?type=wap&ch=1001&info=%E5%85%AC%E4%BA%A425";
            //WebRequest request = WebRequest.Create(url);
            //request.Method = "GET";
            //WebResponse response = request.GetResponse();
            //string responseFromServer = string.Empty;
            //using (Stream reponseStream = response.GetResponseStream())
            //{
            //    StreamReader reader = new StreamReader(reponseStream);
            //    responseFromServer = reader.ReadToEnd();
            //}

        }

        private static void Youdao()
        {
            string url = "http://fanyi.youdao.com/openapi.do?keyfrom=awebsite&key=1521753830&type=data&doctype=json&version=1.1&q=%E6%98%8E%E5%A4%A9%E5%A4%A9%E6%B0%94%E8%BF%98%E5%A5%BD%E5%90%97";
            WebRequest request = WebRequest.Create(url);
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            string responseFromServer = string.Empty;
            using (Stream reponseStream = response.GetResponseStream())
            {
                StreamReader reader = new StreamReader(reponseStream);
                responseFromServer = reader.ReadToEnd();
            }

            System.Text.StringBuilder sb = new System.Text.StringBuilder("");
            sb.AppendLine();
            JToken jtoken = JToken.Parse(responseFromServer);

            sb.AppendLine("---有道翻译---").AppendLine(jtoken.Value<JArray>("translation")[0].Value<string>());

            try
            {
                JArray basicexplains = jtoken.Value<JToken>("basic").Value<JArray>("explains");
                sb.AppendLine("---基本含义---");
                foreach (JToken be in basicexplains)
                {
                    sb.AppendLine(be.Value<string>());
                }

                JToken aweb = jtoken.Value<JArray>("web")[0].Value<JArray>("value");
                sb.AppendLine("---网络释义---");
                foreach (JToken aaweb in aweb)
                {
                    sb.AppendLine(aaweb.Value<string>());
                }
            }
            catch { }

            string content = sb.ToString();
        }
    }
}
